package com.ocm.core.domain;

import java.util.ArrayList;
import java.util.List;

import com.ocm.core.exception.ExceptionCode;
import com.ocm.core.exception.TypeCodeException;
import com.ocm.core.exception.UnsupportTypeException;
import com.ocm.core.factory.ObjectFactory;
import com.ocm.core.service.AssociationTypeService;
import com.ocm.core.service.JCRService;

public enum AssociationType {
   ONE_TO_ONE("1-1"), ONE_TO_MULTIPLE("1-*"), ONE_TO_MULTIPLE_ORDERED_DESC("1-*_desc");

   private String associationType;

   public static final List<AssociationTypeService> associationTypeServices;

   public String getPropertyType() {
      return associationType;
   }

   private AssociationType(String associationType) {
      this.associationType = associationType;
   }

   public static AssociationType getAssociationType(String type) throws UnsupportTypeException {
      AssociationType typeObject = null;
      if ("1-1".equals(type))
         typeObject = AssociationType.ONE_TO_ONE;
      else if (("1-*").equals(type))
         typeObject = AssociationType.ONE_TO_MULTIPLE;
      else if (("1-*_desc").equals(type))
         typeObject = AssociationType.ONE_TO_MULTIPLE_ORDERED_DESC;
      else 
         throw new UnsupportTypeException(ExceptionCode.instance(TypeCodeException.class,
                  TypeCodeException.ASSOCIATIONTYPE));
      return typeObject;
   }

   public AssociationTypeService getAssociationTypeService(JCRService jcrService) throws UnsupportTypeException{
      AssociationTypeService service = null;
      if ("1-1".equals(this.associationType))
         service = ObjectFactory.getOneToOne();
      else if (("1-*").equals(this.associationType))
         service = ObjectFactory.getOneToMultiple();
      else if (("1-*_desc").equals(this.associationType))
         service = ObjectFactory.getOneToMultipleDescOrderation();
      else 
         throw new UnsupportTypeException(ExceptionCode.instance(TypeCodeException.class,
                  TypeCodeException.ASSOCIATIONTYPE));
      service.setJCRService(jcrService);
      return service;
   }

   static {
      associationTypeServices = new ArrayList<AssociationTypeService>();
      associationTypeServices.add(ObjectFactory.getOneToOne());
      associationTypeServices.add(ObjectFactory.getOneToMultiple());
      associationTypeServices.add(ObjectFactory.getOneToMultipleDescOrderation());
   }
}
